import { PURCHASEWAY_OPTIONS } from '@/constant/purchase'
import { CooperateStatusEnum, SUPPLIER_RATING_OPTIONS } from '@/constant/supplier'
import { IAreaType, IOptionArea } from '@/hooks/useGetArea'
import { AreaVerify } from '@/utils/validator'

export const useFormBase = ({
  detailInfo,
  DICTDATA,
  classIdOptions,
  CooperateStatusOptions,
  isEdit,
  cooperateStatus,
}: {
  detailInfo: Record<string, any>
  DICTDATA: IDICTDATAType
  classIdOptions: IOptionItem[]
  CooperateStatusOptions: IOptionItem[]
  isEdit: boolean
  cooperateStatus: number
}): IItemArray[] => {
  return [
    {
      name: 'supplierName',
      label: '供应商名称',
      type: 'Input',
      placeholder: '请输入供应商名称',
      rules: [
        {
          required: true,
          message: '请输入供应商名称',
        },
      ],
      config: {
        showCount: true,
        maxLength: 50,
      },
    },
    {
      name: 'supplierShortName',
      label: '简称',
      type: 'Input',
      placeholder: '请输入简称',
      rules: [
        {
          required: true,
          message: '请输入简称',
        },
      ],
      config: {
        showCount: true,
        maxLength: 10,
      },
    },
    {
      label: '供应商分类',
      [isEdit ? 'other' : 'name']: 'supplierClassificationCode',
      type: isEdit ? 'Custom' : 'Select',
      slot: isEdit ? detailInfo?.supplierClassificationCode || '-' : '',
      option: isEdit ? [] : DICTDATA.supplier_type,
      placeholder: '请选择供应商分类',
      rules: [
        {
          required: true,
          message: '请选择供应商分类',
        },
      ],
      config: {
        optionFilterProp: 'label',
      },
    },
    {
      name: 'purchaseTypes',
      label: '采购模式',
      type: 'Select',
      option: PURCHASEWAY_OPTIONS,
      placeholder: '请选择采购模式',
      config: {
        mode: 'multiple',
        optionFilterProp: 'label',
        maxTagCount: 'responsive',
      },
    },
    {
      name: 'mainClassIds',
      label: '可供应大类',
      type: 'Select',
      option: classIdOptions,
      placeholder: '请选择可供应大类',
      config: {
        mode: 'multiple',
        optionFilterProp: 'label',
        maxTagCount: 'responsive',
      },
    },
    {
      name: 'supplierDetail',
      label: '可供应品类明细',
      type: 'TextArea',
      placeholder: '请输入可供应品类明细',
      config: {
        showCount: true,
        maxLength: 100,
        rows: 1,
      },
    },
    {
      name: 'locationCode',
      label: '区域',
      type: 'Select',
      option: DICTDATA.sp_area,
      placeholder: '请选择区域',
      rules: [
        {
          required: true,
          message: '请选择区域',
        },
      ],
      config: {
        optionFilterProp: 'label',
      },
    },
    {
      name: 'useReason',
      label: '引入原因',
      type: 'TextArea',
      placeholder: '请输入引入原因',
      config: {
        showCount: true,
        maxLength: 200,
        rows: 1,
      },
    },
    {
      label: '开始合作时间',
      [isEdit ? 'other' : 'name']: 'cooperateStartDate',
      slot: isEdit ? detailInfo?.cooperateStartDate || '-' : '',
      type: isEdit ? 'Custom' : 'DatePicker',
      placeholder: '请选择开始合作时间',
      rules: [
        {
          required: true,
          message: '请选择开始合作时间',
        },
      ],
    },
    {
      name: 'cooperateStatus',
      label: '合作状态',
      type: 'Select',
      option: CooperateStatusOptions,
      placeholder: '请选择合作状态',
      rules: [
        {
          required: true,
          message: '请选择合作状态',
        },
      ],
      config: {
        optionFilterProp: 'label',
      },
    },
    {
      label: '结束合作时间',
      name: 'cooperateEndDate',
      type: 'DatePicker',
      placeholder: '请选择结束合作时间',
      rules: [
        {
          required: cooperateStatus == CooperateStatusEnum.OBSOLETE,
          message: '请选择结束合作时间',
        },
      ],
    },
    {
      name: 'levelCode',
      label: '供应商评级',
      type: 'Select',
      option: SUPPLIER_RATING_OPTIONS,
      placeholder: '请选择供应商评级',
      config: {
        optionFilterProp: 'label',
      },
    },
  ]
}
export const useFormInfo = ({
  AreaOptions,
  loadAreaData,
  detailInfo,
}: {
  AreaOptions: IOptionItem[]
  loadAreaData: (info: IOptionArea[]) => void
  detailInfo: Record<string, any>
}): IItemArray[] => {
  return [
    {
      name: 'contactName',
      label: '联系人：',
      type: 'Input',
      placeholder: '请输入联系人',
      rules: [
        {
          required: true,
          message: '请输入联系人',
        },
      ],
    },
    {
      name: 'contactPhone',
      label: '联系方式：',
      type: 'Input',
      slot: detailInfo.contactPhone || '-',
      placeholder: '请输入联系方式',
      rules: [
        {
          required: true,
          message: '请输入联系方式',
        },
      ],
    },
    {
      name: 'contactMail',
      label: '供应商联系邮箱：',
      type: 'TextArea',
      placeholder: '请输入供应商联系邮箱',
      config: {
        showCount: true,
        maxLength: 100,
        rows: 1,
      },
      // rules: [
      //   {
      //     validator: (_: any, value: string) => {
      //       if (!value) {
      //         return Promise.resolve()
      //       }
      //       if (!/^[a-zA-Z0-9_-]+(?:\.[a-zA-Z0-9_-]+)*@[a-zA-Z0-9_-]+(?:\.[a-zA-Z0-9_-]+)+$/.test(value)) {
      //         // 匹配常见邮箱格式（以字母、数字、下划线或中划线开头,点号不在开头或结尾且不连续）
      //         return Promise.reject(new Error('邮箱格式错误！'))
      //       }
      //       return Promise.resolve()
      //     },
      //   },
      // ],
    },
    {
      name: 'legalPerson',
      label: '公司法人：',
      type: 'Input',
      placeholder: '请输入公司法人',
    },
    {
      name: 'legalContactPhone',
      label: '法人电话：',
      type: 'Input',
      placeholder: '请输入法人电话',
    },

    {
      name: 'area',
      label: '省市区：',
      type: 'Cascader',
      slot:
        (detailInfo.provinceName ? detailInfo.provinceName : '') +
        (detailInfo.cityName ? '-' + detailInfo.cityName : '') +
        (detailInfo.areaName ? '-' + detailInfo.areaName : ''),
      option: AreaOptions,
      placeholder: '请选择省市区',
      rules: [
        {
          required: true,
          message: '请选择省市区',
        },
        {
          validator: AreaVerify,
        },
      ],
      config: { loadData: loadAreaData, changeOnSelect: true },
    },
    {
      name: 'contactAddress',
      label: '地址：',
      type: 'Input',
      slot: detailInfo.contactAddress || '-',
      placeholder: '请输入地址',
      rules: [
        {
          required: true,
          message: '请输入地址',
        },
      ],
    },
  ]
}
